package com.belano.provider;

import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import javax.xml.bind.JAXBContext;

import org.springframework.beans.factory.InitializingBean;

import com.belano.domain.Person;
import com.sun.jersey.api.json.JSONConfiguration;
import com.sun.jersey.api.json.JSONJAXBContext;

@Provider
public class JAXBContextResolver implements ContextResolver<JAXBContext>, InitializingBean {

    private JAXBContext context;
    
    private static final Class<?>[] mainTypes = { Person.class };
    
    @Override
    public void afterPropertiesSet() throws Exception {
        JSONConfiguration.Builder confBuilder = JSONConfiguration.mapped()
                .arrays("values", "products", "statuses", "categories")
                .rootUnwrapping(true);

        this.context = new JSONJAXBContext(confBuilder.build(),
                getBoundClasses(), null);
        
    }

    private Class<?>[] getBoundClasses() {        
        return mainTypes;
    }

    @Override
    public JAXBContext getContext(Class<?> arg0) {     
        return context;
    }

}
